package feedback_api

import (
	"github.com/gin-gonic/gin"
	"gvb_server/global"
	"gvb_server/models"
	"gvb_server/models/res"
)

type FeedbackRequest struct {
	Email   string `json:"email" binding:"required,email" msg:"邮箱非法" structs:"email"`
	Content string `json:"content" binding:"required" msg:"请输入反馈内容" structs:"content"`
}

func (FeedbackApi) FeedBackCreateView(c *gin.Context) {
	var cr FeedbackRequest
	err := c.ShouldBindJSON(&cr)
	if err != nil {
		res.FailWithError(err, &cr, c)
		return
	}
	// 重复的判断
	var feedback models.FeedbackModel
	err = global.DB.Take(&feedback, "email = ? and content = ?", cr.Email, cr.Content).Error
	if err == nil {
		res.FailWithMessage("您已经反馈过该问题", c)
		return
	}

	err = global.DB.Create(&models.FeedbackModel{
		Email:   cr.Email,
		Content: cr.Content,
	}).Error

	if err != nil {
		global.Log.Error(err)
		res.FailWithMessage("添加反馈失败", c)
		return
	}

	res.OkWithMessage("添加反馈成功", c)
}
